<?php 
include("header.php");
?>

<?php 
function GetArticleMeta($aid) {
  $meta = null;
  if (!($meta = apc_fetch(sprintf('article-meta-%d', $aid)))) {
    $sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.url AS a_url, a.category AS a_category, a.create_time AS a_create_time,
      u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url
      FROM articles AS a, authors AS u WHERE a.author = u.id AND a.id = %d", $aid);
    // print $sql;
    $result = mysql_query($sql);
    $meta = mysql_fetch_array($result);
    if ($meta && $meta['a_title']) {
      apc_add(sprintf('article-meta-%d', $aid), $meta, 3600 * 2);
    }
  }
  return $meta;
}

function PageNav($max_page, $current_page) {
  $max_dist = 4;
  $i = $current_page - $max_dist;
  if ($i < 1) {
    $i = 1;
  }
  $last_page = $current_page + $max_dist;
  if ($last_page > $max_page) {
    $last_page = $max_page;
  }

  if ($current_page > 1) {
    $prev = $current_page - 1;
    printf('<a class="page-nav" href="/page/%d/?f=prev">上一页</a> ', $prev + 1);
  }

  for(; $i <= $last_page; ++$i) {
    if($i == $current_page) {
      printf('<a class="page-nav current" href="/page/%d/" title="第%d页">%d</a> ', $i, $i, $i);
    } else {
      printf('<a class="page-nav" href="/page/%d/" title="第%d页">%d</a> ', $i, $i, $i);
    }
  }

  if ($current_page < $max_page) {
    $next = $current_page + 1;
    printf('<a class="page-nav" href="/page/%d/?f=next">下一页</a>', $next + 1);
  }
  printf('(共%d页)', $max_page);
}

$page = intval($_GET['page']);
if ($page == null) {
  $page = 1;
}

$count_per_page = 40;

$conn = mysql_connect("127.0.0.1","root","");
mysql_select_db("xingtan");

$sql=sprintf("SELECT count(a.id) AS total FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category >= 0");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$max_page = ceil($row['total'] / $count_per_page);

$sql=sprintf("SELECT a.id AS a_id, a.title AS a_title, a.digest AS a_digest, a.create_time AS a_create_time, 
    u.id AS u_id, u.name AS u_name, u.title AS u_title, u.url AS u_url 
    FROM articles AS a, authors AS u WHERE a.author = u.id AND a.category >= 0 ORDER BY a.id DESC limit %d", $page * $count_per_page);
$result = mysql_query($sql);
?>

	<div class="main">
		<div class="content">
			<div class="articles">
<?php 

function TruncateStr($str, $max_len) {
    return $str;
    if (mb_strlen($str, 'utf-8') > $max_len) {
       return mb_substr($str, 0, $max_len - 1, 'utf-8') . '...';
    }
    return $str;
}

$index = 0;
while($row = mysql_fetch_array($result)) {
  if($index >= ($page - 1) * $count_per_page) {
    printf('<div>');
    if (false && $row['u_id'] > 0) {
      printf('<div style="float:right;font-size:12px;line-height:22px;margin-right:8px;"><a title="%s" href="%s">%s</a></div>', $row['u_title'], $row['u_url'], $row['u_name']);
    }
    printf('<h3 class="article-title"><a title="%s" target="_blank" href="/a/%d.html">%s</a></h3>', $row['a_create_time'], $row['a_id'], TruncateStr($row['a_title'], 24));
    printf('</div>');
  }
  ++$index;
}

print('<hr/>');
PageNav($max_page, $page);
?>
</div>
</div>

<?php 
mysql_close($conn);
include("footer.php");
?>
